package csu.web.mypetstore.web.servlet;

import csu.web.mypetstore.domain.Order;
import csu.web.mypetstore.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class ConfirmOrderServlet extends HttpServlet {
    private String msg;
    private static  final  String NEW_ORDER_FORM = "/WEB-INF/jsp/order/newOrder.jsp";
    private static final String CONFIRM_ORDER_FORM = "/WEB-INF/jsp/order/confirmOrder.jsp";
    private static final String SHIPPING_FORM = "/WEB-INF/jsp/order/shipping.jsp";
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        Order order = (Order) session.getAttribute("order");
        if (order != null) {
            String shippingAddressRequired = req.getParameter("shippingAddressRequired");
            // 如果复选框被勾选
            if (shippingAddressRequired != null && shippingAddressRequired.equals("on")) {
                req.getRequestDispatcher(SHIPPING_FORM).forward(req,resp);
               //resp.sendRedirect("shippingForm");
            } else {
                // 如果复选框未被勾选
                String billToFirstName = req.getParameter("billToFirstName");
                String billToLastName = req.getParameter("billToLastName");
                String billAddress1 = req.getParameter("billAddress1");
                String billAddress2 = req.getParameter("billAddress2");
                String billCity = req.getParameter("billCity");
                String billState = req.getParameter("billState");
                String billZip = req.getParameter("billZip");
                String billCountry = req.getParameter("billCountry");
                //有输入内容为空的情况
                if (billToFirstName.isEmpty() || billToLastName.isEmpty() || billAddress1.isEmpty() || billAddress2.isEmpty()
                        || billCity.isEmpty() || billState.isEmpty() || billZip.isEmpty() || billCountry.isEmpty()) {
                        this.msg = "订单内容不能为空";
                        req.setAttribute("orderMsg", this.msg);
                        req.getRequestDispatcher(NEW_ORDER_FORM).forward(req,resp);
                } else {
                    order.setBillToFirstName(billToFirstName);
                    order.setBillToLastName(billToLastName);
                    order.setBillAddress1(billAddress1);
                    order.setBillAddress2(billAddress2);
                    order.setBillCity(billCity);
                    order.setBillState(billState);
                    order.setBillZip(billZip);
                    order.setBillCountry(billCountry);

                    session.setAttribute("order",order);
                    OrderService orderService = new OrderService();
                    orderService.insertOrder(order);
                    req.getRequestDispatcher(CONFIRM_ORDER_FORM).forward(req, resp);
                }
            }
        }
    }
}

